#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>

#include <time.h>
#include <ctype.h>
#include <stddef.h>
#include <stdio.h>
#include <stdint.h>
#include "os_api.h"
#include "debug.h"
#include "spi.h"


#define SPI_STACK_SIZE (3*1024)

static void *spi_task_stack = NULL;

static OSTaskRef spi_task_ref = NULL;

static void spi_task(void *ptr)
{
	int ret = 0;
	unsigned char writeBuf[30] = {0};
	unsigned char readBuf[30] = {0};

	ret = hal_SpiInit(SPI_PORT3, SPI_MODE3, SPI_CLK_3_25MHZ, FALSE); 
	LOG_PRINTF("hal_SpiInit ret %d", ret);

	while (1)
	{

		memset(writeBuf, 0x00, sizeof(writeBuf));
		memset(readBuf, 0x00, sizeof(readBuf));

		writeBuf[0] = 0xA1;
		writeBuf[1] = 0xA2;
		writeBuf[2] = 0xA3;
		writeBuf[3] = 0xA4;
		writeBuf[4] = 0xA5;
		writeBuf[5] = 0xA6;
		writeBuf[6] = 0xA7;
		writeBuf[7] = 0xA8;
		writeBuf[8] = 0xA9;
		writeBuf[9] = 0xAA;

		ret = hal_SpiWriteRead(SPI_PORT3, readBuf, writeBuf, strlen((char *)writeBuf));
		//ret = hal_SpiWriteReadDma(SPI_PORT3, readBuf, writeBuf, strlen((char *)writeBuf));
		LOG_PRINTF("hal_SpiWriteRead ret %d, readBuf %02X,%02X,%02X\n", ret, readBuf[0], readBuf[1], readBuf[2]);

		OSATaskSleep(5 * 200);
	}
}


int main(void)
{
	int ret;

	spi_task_stack = malloc(SPI_STACK_SIZE);
	ASSERT(spi_task_stack != NULL);

	ret = OSATaskCreate(&spi_task_ref, spi_task_stack, SPI_STACK_SIZE, 98, "spi_task", spi_task, NULL);
	ASSERT(ret == OS_SUCCESS);

	return 0;
}
